package org.example.statistics.controller;

import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import org.example.statistics.domain.StatisticsSell;
import org.example.statistics.service.IStatisticsSellService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 统计销售报表Controller
 *
 * @author 小孟微信：xiaomengcode
 * @date 2024-05-31
 */
@Controller
@RequestMapping("/statistics/statisticsSell")
public class StatisticsSellController extends BaseController
{
    private String prefix = "statistics/statisticsSell";

    @Autowired
    private IStatisticsSellService statisticsSellService;

    @GetMapping()
    public String statisticsSell(StatisticsSell sell,ModelMap mp)
    {
        mp.put("sell",sell);
        return prefix + "/statisticsSell";
    }

    /**
     * 查询统计销售报表列表
     */
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(StatisticsSell statisticsSell)
    {
        startPage();
        List<StatisticsSell> list = statisticsSellService.selectStatisticsSellList(statisticsSell);
        return getDataTable(list);
    }

    /**
     * 导出统计销售报表列表
     */
    @RequiresPermissions("statistics:statisticsSell:export")
    @Log(title = "统计销售报表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(StatisticsSell statisticsSell)
    {
        List<StatisticsSell> list = statisticsSellService.selectStatisticsSellList(statisticsSell);
        ExcelUtil<StatisticsSell> util = new ExcelUtil<StatisticsSell>(StatisticsSell.class);
        return util.exportExcel(list, "统计销售报表数据");
    }

    /**
     * 新增统计销售报表
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存统计销售报表
     */
    @RequiresPermissions("statistics:statisticsSell:add")
    @Log(title = "统计销售报表", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(StatisticsSell statisticsSell)
    {
        return toAjax(statisticsSellService.insertStatisticsSell(statisticsSell));
    }

    /**
     * 修改统计销售报表
     */
    @RequiresPermissions("statistics:statisticsSell:edit")
    @GetMapping("/edit/{statisticsSellId}")
    public String edit(@PathVariable("statisticsSellId") Long statisticsSellId, ModelMap mmap)
    {
        StatisticsSell statisticsSell = statisticsSellService.selectStatisticsSellByStatisticsSellId(statisticsSellId);
        mmap.put("statisticsSell", statisticsSell);
        return prefix + "/edit";
    }

    /**
     * 修改保存统计销售报表
     */
    @RequiresPermissions("statistics:statisticsSell:edit")
    @Log(title = "统计销售报表", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(StatisticsSell statisticsSell)
    {
        return toAjax(statisticsSellService.updateStatisticsSell(statisticsSell));
    }

    /**
     * 删除统计销售报表
     */
    @RequiresPermissions("statistics:statisticsSell:remove")
    @Log(title = "统计销售报表", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(statisticsSellService.deleteStatisticsSellByStatisticsSellIds(ids));
    }


    @RequestMapping( "/sell")
    @ResponseBody
    public AjaxResult sell(StatisticsSell statisticsSell)
    {
        List<StatisticsSell> statisticsSells = statisticsSellService.queryStatisticsSellByGroupByMonth(statisticsSell);
        return AjaxResult.success(statisticsSell);
    }

    @RequestMapping( "/currentMonth")
    @ResponseBody
    public AjaxResult selectStatisticsSellByCurrentMonth(StatisticsSell statisticsSell)
    {
        StatisticsSell sell = statisticsSellService.selectStatisticsSellByCurrentMonth(statisticsSell);
        if(sell ==null){
            sell =new StatisticsSell();
        }
        return AjaxResult.success(sell);
    }


}
